home *** CD-ROM | disk | FTP | other *** search
- Path: news2.aimnet.com!usenet
- From: Dan Howard <dhoward@kset.com>
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.tools.mfc
- Subject: Re: Newbie question: default op=
- Date: 19 Jan 1996 01:16:46 GMT
- Organization: Aimnet Information Services
- Message-ID: <4dmrdu$niv@news2.aimnet.com>
- References: <30FEA0B4.4F66@ymi.com>
- NNTP-Posting-Host: host110.kset.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
-
- Sadly, you are correct. You do need to write an explicit operator =() because the compiler
- does not provide one. Yes, the compiler should be able to figure it out but it isn't in the
- Draft ANSI C++ Specification so too bad.
-
- If you want an even worse problem, the copy constructor automatically provided by compilers
- does a bit-copy rather than a member-by-member copy. Thus, no copy constructors are called in
- the following code:
-
- class stupid
- {
- CString x;
- };
-
- void main()
- {
- stupid a;
-
- stupid b(a); // try stepping into this line, no CString::CString(const CString& s) called
-
- return;
- }
-
- That's the ANSI spec. That is why it is always recommended that you declare a copy
- constructor. If you declare and don't define it, the linker will complain. If you don't
- declare or define it, the linker will substitute a bit-copy and (sometimes) confuse the shit
- out of you for several hours.
-
- Jeffrey Keays <keays@ymi.com> wrote:
- >I am trying to use the &*%$ MFC collections in some way that will dispose of its contents on destruction. In
- >my 2-days and counting of ramming my head repeatedly against this brick wall over something that should be made
- >'easier' by them, I have this general C++ question.
- >
- >Why is it making me create operator= functions for assignment of the same type (or instance / reference of the
- >same type). The class I'm trying to assign has no pointers or anything that would require special handling.
- >Why do I have to write stupid things like
- >
- >class foo {
- > int data1;
- > double data2;
- > CString data3;
- > // ... ad infinitum
- >public:
- > foo& operator=(const foo &);
- >}
- >
- >foo& foo::operator=(const foo &rhs)
- >{
- > data1 = rhs.data1
- > data2 = rhs.data2
- > data3 = rhs.data3
- > // ... -- I should not have to write this, it should be ASSUMED
- > // by the compiler if I don't specify otherwise.
- >}
- >
-
-
-